package reduce.fz.com.reduce.util;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by qingshengzheng on 15/6/23.
 */
public class DateUtil {
    /**
     * 英文简写（默认）如：2010-12-01
     */
    public static String FORMAT_SHORT = "yyyy-MM-dd";

    /**
     * 英文全称  如：2010-12-01 23:15:06
     */
    public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";

    /**
     * 精确到毫秒的完整时间    如：yyyy-MM-dd HH:mm:ss.S
     */
    public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";

    /**
     * 中文简写  如：2010年12月01日
     */
    public static String FORMAT_SHORT_CN = "yyyy年MM月dd";

    /**
     * 中文全称  如：2010年12月01日  23时15分06秒
     */
    public static String FORMAT_LONG_CN = "yyyy年MM月dd日  HH时mm分ss秒";

    /**
     * 精确到毫秒的完整中文时间
     */
    public static String FORMAT_FULL_CN = "yyyy年MM月dd日  HH时mm分ss秒SSS毫秒";


    /**
     * 英文简写（默认）如：12-01
     */
    public static String MONTH_DAY = "MM-dd";

    /**
     * 英文简写（默认）如：12-01
     */
    public static String HOUR_MINUTE = "hh:mm";

    public static String HOUR_MINUTE2 = "MM.dd";


    /**
     * 获取时间戳
     */
    public static String getTimeString() {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
        Calendar calendar = Calendar.getInstance();
        return df.format(calendar.getTime());
    }

    /**
     * 获取日期年份
     *
     * @param date 日期
     * @return
     */
    public static String getYear(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
        return df.format(date).substring(0, 4);
    }

    /**
     * 功能描述：返回月
     *
     * @param date Date 日期
     * @return 返回月份
     */
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 功能描述：返回日
     *
     * @param date Date 日期
     * @return 返回日份
     */
    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 功能描述：返回小
     *
     * @param date 日期
     * @return 返回小时
     */
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 功能描述：返回分
     *
     * @param date 日期
     * @return 返回分钟
     */
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 返回秒钟
     *
     * @param date Date 日期
     * @return 返回秒钟
     */
    public static int getSecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }

    /**
     * 功能描述：返回毫
     *
     * @param date 日期
     * @return 返回毫
     */
    public static long getMillis(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }



    public static  long getTimeStamp()
    {
        return System.currentTimeMillis();
    }

    public static String formatSecond(Double second) {
        String html = "0秒";
        if (second != null) {
            Double s = second;
            String format;
            Object[] array;
            Integer hours = (int) (s / (60 * 60));
            Integer minutes = (int) (s / 60 - hours * 60);
            Integer seconds = (int) (s - minutes * 60 - hours * 60 * 60);
            if (hours > 0) {
                format = "%1$,d时%2$,d分%3$,d秒";
                array = new Object[]{hours, minutes, seconds};
            } else if (minutes > 0) {
                format = "%1$,d分%2$,d秒";
                array = new Object[]{minutes, seconds};
            } else {
                format = "%1$,d秒";
                array = new Object[]{seconds};
            }
            html = String.format(format, array);
        }

        return html;

    }

    public static String formatMonthDay(long time) {
        DateFormat formatter = new SimpleDateFormat(MONTH_DAY);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return formatter.format(calendar.getTime());
    }

    public static String formatMonthDay2(long time) {
        DateFormat formatter = new SimpleDateFormat(MONTH_DAY);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return formatter.format(calendar.getTime());
    }

    public static String formatMonthDay(String time) {

        long record_time = Long.parseLong(time);
        DateFormat formatter = new SimpleDateFormat(MONTH_DAY);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(record_time);
        return formatter.format(calendar.getTime());
    }


    public static String formatHourMinute(long time) {
        DateFormat formatter = new SimpleDateFormat(HOUR_MINUTE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return formatter.format(calendar.getTime());
    }

    public static String formatHourMinute(String time) {
        long record_time = Long.parseLong(time);
        DateFormat formatter = new SimpleDateFormat(HOUR_MINUTE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(record_time);
        return formatter.format(calendar.getTime());
    }


    public static String getTodyTime() {
        Date dt = new Date();
        DateFormat formatter = new SimpleDateFormat(MONTH_DAY);

        String nowTime = formatter.format(dt);
        return nowTime;
    }

    /**
     * 获得今天在内的之前7天时间
     *
     * @return
     */
    public static String[] get7DayTime() {

        String[] time7Day = new String[7];
        for (int i = 6; i >= 0; i--) {
            Date nowDate = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(nowDate);
            calendar.add(Calendar.DAY_OF_MONTH, i - 6);
            Date DDday = calendar.getTime();
            DateFormat formatter = new SimpleDateFormat(HOUR_MINUTE2);
            String Time = formatter.format(DDday);
            time7Day[i] = Time;
        }
        return time7Day;

    }

    /**
     * 获得指定天数在内的之前7天时间
     *
     * @return
     */
    public static String[] get7DayTime(String data) {


        SimpleDateFormat formatter = new SimpleDateFormat(MONTH_DAY);
        Date datenew = null;
        try {
            datenew = formatter.parse(data);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String[] time7Day = new String[7];
        for (int i = 0; i < 7; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(datenew);
            calendar.add(Calendar.DAY_OF_MONTH, -i);
            Date DDday = calendar.getTime();
            DateFormat formatte = new SimpleDateFormat(MONTH_DAY);
            String Time = formatte.format(DDday);
            time7Day[i] = Time;
        }
        return time7Day;
    }


    public static String formatDuring(int mss) {
        int hours = (mss % (60 * 24)) / (60);
        int minutes = (mss % (60));

        if (hours == 0) {
            return minutes + "分";
        }

        return hours + "小时" + minutes + "分";
    }

    public static String formatSleepSum(int mss) {
        int hours = (mss % (60 * 24)) / (60);
        int minutes = (mss % (60));

        if (hours == 0) {
            return minutes + "分";
        }

        return hours + ":" + minutes;
    }


    public static String formatTrackTime(long mss) {
        String hour;
        String minute;
        int hours = (int) (mss % (60 * 24)) / (60);
        int minutes = (int) (mss % (60));

        if (hours < 10) hour = "0" + hours;
        else hour = String.valueOf(hours);
        if (minutes < 10) minute = "0" + minutes;
        else minute = String.valueOf(minutes);

        return hour + ":" + minute;
    }

    public static String getDistance(float distance) {

        float distan = distance * 1.0f / 1000;
        DecimalFormat df = new DecimalFormat("#0.00");
        String str = df.format(distan);

        return str;
    }

    public static float getDistancefloat(float distance) {

        float distan = distance / 1000;
        DecimalFormat df = new DecimalFormat("#.00");
        String str = df.format(distan);

        return Float.valueOf(str);
    }

    public static float formatTrackData(double duration) {

        float distan = (float) (duration / 60);
        DecimalFormat df = new DecimalFormat("#.00");
        String str = df.format(distan);

        return Float.valueOf(str);
    }

    public static String formatTrackData2(double second) {
        String html = "00:00";
        if (second != 0) {
            Double s = second;
            Integer hours = (int) (s / (60 * 60));
            Integer minutes = (int) (s / 60 - hours * 60);
            Integer seconds = (int) (s - minutes * 60 - hours * 60 * 60);
            String minute;
            String secon;
            if (minutes < 10) minute = "0" + minutes;
            else minute = String.valueOf(minutes);
            if (seconds < 10) secon = "0" + seconds;
            else secon = String.valueOf(seconds);
            if (hours > 0) {
                html = hours + ":" + minute + ":" + secon;
                return html;
            } else if (minutes > 0) {
                html = "00:" + minute + ":" + secon;
                return html;
            } else {
                html = "00:00:" + secon;
                return html;
            }
        }

        return html;

    }

}
